<?php

class Admin extends Controller {

    function Admin() {
        parent::Controller();
        $this->load->model(array('mod_login'));
        $this->load->library('session');
        $this->load->library('form_validation');
    }
//===============Function load page master================
    function index() {
        if ($this->check_session()) {
            $this->load->view('master');
        } else {
            $this->load->view('admin/login');
        }
    }

//==========this is function for login check=======================

    public function login() {
        $this->form_validation->set_rules('txtusername', 'username', 'required|trim');
        $this->form_validation->set_rules('txtpassword', 'password', 'required|trim');
        if ($this->form_validation->run() == false) {
           
            $this->load->view('admin/login');

//=========if form validation is false===================
        } else {
            if ($this->check_session() || $this->mod_login->use_login($this->input->post('txtusername'), $this->input->post('txtpassword'))) {
                redirect('admin/');
            } else {
                $this->session->set_flashdata('msg_login_err', 'Invalid username or password!');
                redirect('admin/');
            }
        }
    }

//====================function for destroy session when it was loged out===================

    function logout() {
        $this->session->sess_destroy();
        redirect('admin/');
    }

//========this is function for using checking session=============


    public function check_session() {
        if ($this->session->userdata('use_username')) {
            return true;
        } else {
            return false;
        }
    }

}